package cn.zzuli.jsjiexi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
List<ArticleBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        String html="{\"data\":[{\"desc\":\"扔物线\",\"id\":29,\"title\":\"View 嵌套\", \"url\":\"https://www.bilibili.com/video/BV1ZA41137gr\"}],\"errorCode\":0,\"errorMsg\":\"\"}";
        list=new ArrayList<ArticleBean>();
        list = parseArticle(html);
        System.out.println("==================");
        System.out.println(list);

    }

    List<ArticleBean> parseArticle(String strJson)  {
        List<ArticleBean> articles = new ArrayList<>();
        JSONObject obj = null;
        try {
            obj = new JSONObject(strJson);
            JSONArray arr = obj.optJSONArray("data");
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj1 = arr.optJSONObject(i);
                int id = obj1.getInt("id");
                String desc = obj1.getString("desc");
                String title = obj1.getString("title");
                String url = obj1.getString("url");
                ArticleBean article = new ArticleBean(id, title, desc, url);
                articles.add(article);
                System.out.println("===0000=====");
                System.out.println(article);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return articles;
    }


}